<template>
  <div class="photo-masonry">
    <div class="masonry-columns">
      <div
        v-for="(photo, index) in photos"
        :key="photo.id"
        class="photo-item"
        @click="$emit('preview', index)"
      >
        <div class="photo-wrapper">
          <img
            :src="photo.url"
            :alt="photo.title || '照片'"
            loading="lazy"
            :data-index="index"
            class="gallery-img"
          />
          <div class="photo-info">
            <h3 class="photo-title">{{ photo.title }}</h3>
            <div class="photo-actions">
              <button class="action-btn preview" @click.stop="$emit('preview', index)">
                <i class="ri-eye-line"></i>
              </button>
              <button
                class="action-btn download desktop-only"
                @click.stop="$emit('download', photo)"
              >
                <i class="ri-download-line"></i>
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
defineProps({
  photos: {
    type: Array,
    required: true,
  },
})

defineEmits(['preview', 'download'])

defineOptions({ name: 'PhotoMasonry' })
</script>

<style lang="scss" scoped>
.photo-masonry {
  .masonry-columns {
    column-count: 4; // 默认4列
    column-gap: 1.5rem;

    @media (max-width: 1200px) {
      column-count: 3; // 中等屏幕3列
    }

    @media (max-width: 900px) {
      column-count: 2; // 小屏幕2列
      column-gap: 1rem;
    }

    @media (max-width: 600px) {
      column-count: 2; // 移动设备2列
      column-gap: 0.7rem;
    }

    @media (max-width: 500px) {
      column-count: 1; // 移动设备1列
      column-gap: 0;
    }
  }

  .photo-item {
    break-inside: avoid; // 防止内容被分割到不同列
    display: inline-block; // 必须设置以确保正确的列布局
    width: 100%;
    margin-bottom: 1.5rem;
    padding: 0 0.25rem;

    @media (max-width: 900px) {
      margin-bottom: 1rem;
    }

    .photo-wrapper {
      position: relative;
      overflow: hidden;
      border-radius: 0.75rem;
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
      transition:
        transform 0.3s ease,
        box-shadow 0.3s ease;

      &:hover {
        transform: translateY(-6px);
        box-shadow: 0 12px 24px rgba(0, 0, 0, 0.12);

        img {
          transform: scale(1.03);
        }

        .photo-info {
          opacity: 1;
        }
      }

      img {
        display: block;
        width: 100%;
        height: auto;
        transition: transform 0.5s ease;
      }

      .photo-info {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        padding: 1rem;
        background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
        color: white;
        opacity: 0;
        transition: opacity 0.3s ease;
        display: flex;
        justify-content: space-between;
        align-items: flex-end;

        .photo-title {
          font-size: 0.9375rem;
          font-weight: 500;
          margin: 0;
          text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
          flex: 1;
        }

        .photo-actions {
          display: flex;
          gap: 0.5rem;

          .action-btn {
            width: 32px;
            height: 32px;
            background: rgba(255, 255, 255, 0.2);
            backdrop-filter: blur(4px);
            border: none;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            transition: all 0.2s ease;

            i {
              font-size: 1rem;
              color: white;
            }

            &:hover {
              background: rgba(255, 255, 255, 0.4);
              transform: scale(1.1);
            }
          }
        }
      }
    }
  }
}

// 响应式调整
@media (max-width: 768px) {
  .photo-masonry {
    .photo-item {
      padding: 0;

      .photo-wrapper {
        border-radius: 0.5rem;

        .photo-info {
          padding: 0.75rem;

          .photo-title {
            font-size: 0.875rem;
          }

          .photo-actions {
            .action-btn {
              width: 28px;
              height: 28px;
            }
          }
        }
      }
    }
  }

  // 移动端隐藏下载按钮
  .desktop-only {
    display: none !important;
  }
}
</style>
